package com.temp.sqlite;

import java.sql.*;

public class SqlLiteMain {
    public static void main(String[] args) {
        // 指定数据库文件路径
        String dbPath = "sqlite/database.db"; // 修改为你的路径

        // 使用JDBC连接SQLite数据库
        String url = "jdbc:sqlite:" + dbPath;

        try {
            // 加载SQLite JDBC驱动
            Class.forName("org.sqlite.JDBC");
            // 建立连接，这里假设数据库文件名为test.db，位于当前目录下
            Connection conn = DriverManager.getConnection(url);
            System.out.println("成功连接到SQLite数据库");
            // 示例：创建表
            String sql = "CREATE TABLE IF NOT EXISTS users (id LONG PRIMARY KEY, name TEXT)";
            try (Statement stmt = conn.createStatement()) {
                stmt.execute(sql);
                conn.setAutoCommit(false);
                Long id = System.currentTimeMillis();
                stmt.execute(" insert into users(id,name) values(" + id + ",'" + id + "')");
                System.out.println("***********"+id);
                if(System.currentTimeMillis() %11 < 5){
                    System.out.println("*********** commit");
                    conn.commit();
                } else {
                    System.out.println("*********** rollback");
                    conn.rollback();
                }
                ResultSet rs = stmt.executeQuery("select * from users");
                while (rs.next()) {
                    System.out.println(rs.getLong(1) + " ***  " + rs.getString(2));
                }

            }

            // 关闭连接
            conn.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}